package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.TrainExcludeTeacher;
import com.xmy.cultivate.entity.TrainPrepare;
import com.xmy.cultivate.service.ITrainExcludeTeacherService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * <p>
 * 排除需要统计的教师 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-17
 */
@RestController
@RequestMapping("/api/trainExcludeTeacher")
public class TrainExcludeTeacherController {
    @Autowired
    ITrainExcludeTeacherService trainExcludeTeacherService;

    @GetMapping("/trainExcludeTeacherList")
    public Result trainExcludeTeacherList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "teacherName", required = false) String teacherName,
                                          @RequestParam(value = "videoType", required = false) String videoType,
                                          @RequestParam(value = "yearPart", required = false) String yearPart,
                                          @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                          @RequestParam(value = "subjectsId", required = false) Long subjectsId){
        IPage<TrainExcludeTeacher> iPage = new Page<>(pageNum, pageSize);
        trainExcludeTeacherService.getTrainExcludeTeacherList(iPage, teacherName, videoType, subjectsId, yearPart, quarterNum);
        for (TrainExcludeTeacher trainExcludeTeacher : iPage.getRecords()){
            //获取年级
            String yearClassIdStr = trainExcludeTeacher.getYearClassIdStr();
            if (yearClassIdStr != null && !yearClassIdStr.isEmpty()) {
                String[] yearClassIdArray = yearClassIdStr.split(",");
                StringJoiner yearClassIdStrName = new StringJoiner(",");
                for (String yearClassId : yearClassIdArray) {
                    if (yearClassId != null && !yearClassId.isEmpty()) {
                        try {
                            yearClassIdStrName.add(CommonUtil.getYearClassNameForId(Integer.valueOf(yearClassId)));
                        } catch (NumberFormatException e) {
                            yearClassIdStrName.add("未知年级");
                        }
                    }
                }
                trainExcludeTeacher.setYearClassStrName(yearClassIdStrName.toString());
            }

        }
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }
    @PostMapping("/saveTrainExcludeTeacher")
    public Result saveTrainExcludeTeacher(@RequestBody TrainExcludeTeacher trainExcludeTeacher){
        QueryWrapper<TrainExcludeTeacher> qw = new QueryWrapper<>();
        qw.eq("year_part", trainExcludeTeacher.getYearPart());
        qw.eq("quarter_num", trainExcludeTeacher.getQuarterNum());
        qw.eq("teacher_id", trainExcludeTeacher.getTeacherId());
        qw.eq("subjects_id",  trainExcludeTeacher.getSubjectsId());
        qw.eq("video_type", trainExcludeTeacher.getVideoType());
        long count = trainExcludeTeacherService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }

        boolean save = trainExcludeTeacherService.save(trainExcludeTeacher);
        if (save){
            return new Result(Code.OK, "保存成功");
        }
        return new Result(Code.ERROR, "保存失败");
    }

    @DeleteMapping("/deleteTrainExcludeTeacher")
    public Result deleteTrainExcludeTeacher(@RequestBody Map reMap){
        List<String> ids = (List<String>) reMap.get("ids");
        if (ids == null || ids.isEmpty()){
            return new Result(Code.DELETE_ERROR, "", "请选择要删除的id");
        }
        QueryWrapper<TrainExcludeTeacher> qw = new QueryWrapper<>();
        qw.in("id",ids);
        boolean remove = trainExcludeTeacherService.remove(qw);
        if (remove) {
            return new Result(Code.OK, null, Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.DELETE_ERROR_MSG);
    }

    @PutMapping("/editTrainExcludeTeacher")
    public Result editTrainExcludeTeacher(@RequestBody TrainExcludeTeacher trainExcludeTeacher){
        TrainExcludeTeacher trainExcludeTeacherOld = trainExcludeTeacherService.getById(trainExcludeTeacher.getId());
        if (Objects.isNull(trainExcludeTeacherOld) ){
            return new Result(Code.ERROR, null, "该数据不存在,编辑失败");

        }
        QueryWrapper<TrainExcludeTeacher> qw = new QueryWrapper<>();
        qw.ne("id", trainExcludeTeacher.getId());
        qw.eq("year_part", trainExcludeTeacherOld.getYearPart());
        qw.eq("quarter_num", trainExcludeTeacherOld.getQuarterNum());
        qw.eq("teacher_id", trainExcludeTeacher.getTeacherId());
        qw.eq("subjects_id",  trainExcludeTeacher.getSubjectsId());
        qw.eq("video_type", trainExcludeTeacherOld.getVideoType());
        long count = trainExcludeTeacherService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }

        trainExcludeTeacherOld.setTeacherId(trainExcludeTeacher.getTeacherId());
        trainExcludeTeacherOld.setSubjectsId(trainExcludeTeacher.getSubjectsId());
        trainExcludeTeacherOld.setYearClassIdStr(trainExcludeTeacher.getYearClassIdStr());
        boolean update = trainExcludeTeacherService.updateById(trainExcludeTeacherOld);
        if (update){
            return new Result(Code.OK, "修改成功");
        }
        return new Result(Code.ERROR, "修改失败");
    }

}
